package com.example.controller;

import com.example.Service.CampusResourceService;
import com.example.entity.CampusResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resources")
@CrossOrigin(origins = "*")
public class CampusResourceController {

    @Autowired
    private CampusResourceService campusResourceService;

    @PostMapping("/add")
    public String addResource(@RequestBody CampusResource resource) {
        return campusResourceService.addResource(resource) > 0 ? "success" : "fail";
    }

    @DeleteMapping("/delete/{resourceId}")
    public String deleteResource(@PathVariable Integer resourceId) {
        return campusResourceService.deleteResourceById(resourceId) > 0 ? "success" : "fail";
    }

    @PutMapping("/update")
    public String updateResource(@RequestBody CampusResource resource) {
        return campusResourceService.updateResource(resource) > 0 ? "success" : "fail";
    }

    @GetMapping("/all")
    public List<CampusResource> getAllResources() {
        return campusResourceService.getAllResources();
    }
}
